<?php

namespace Summer\Box\Http\Controllers;

use Illuminate\Routing\Controller;
use RainLab\User\Models\User as UserModel;
use RLuders\JWTAuth\Classes\JWTAuth;
use Input;
use Config;
use Db;
use Event;
use Exception;
use Summer\Box\Models\BoxOrderModel;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;

class UserController extends Controller
{
    use ApiReturn;

    /**
     * 商城相关用户信息
     * @param JWTAuth $auth
     * @return \Illuminate\Http\JsonResponse
     */
    public function userData(JWTAuth $auth)
    {
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $order_num=[];
        $order_status_config=Config::get('summer.box::order_status');
        foreach ($order_status_config as $osck=>$oscv){
            $order_num[$osck]=BoxOrderModel::where('buy_uid',$user->id)->where('status',$osck)->count();
        }
        $order_num[5]=BoxOrderModel::where('sell_uid',$user->id)->whereIn('status',[1,2])->count();
        $return['order_num']=$order_num;
        return $this->apiSuccess('操作成功',$return);
    }

    /**
     * 团队业绩统计
     * @param JWTAuth $auth
     * @return \Illuminate\Http\JsonResponse
     */
    public function tdYeji(JWTAuth $auth){
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $page_size=Input::get('page_size')??20;
        $page=Input::get('page')??1;

        $tj_type=Input::get('tj_type')??'today';
        //推荐的ids
        $tjdids=[];
        $tjdids=UserModel::where('tuid',$user->id)->pluck('id');

        //团队列表
        $query=UserModel::select('id','username','userphone','realname','nickname','tdeep','created_at','yeji_td','yeji_td_total','yeji_self','yeji_self_total');
        if($tj_type=='today'){
            $query->where('yeji_self','>',0);
        }else{
            $query->where('yeji_self_total','>',0);
        }
        $records=$query->whereRaw("FIND_IN_SET(?,`tids`)",[$user->id])->paginate($page_size,$page);
        $records->each(function ($record)use($tj_type){
            if($tj_type=='today'){
                $record->show_yeji=$record->yeji_self;
            }else{
                $record->show_yeji=$record->yeji_self_total;
            }

        });
        $rdata['records']=$records;
        return $this->apiSuccess('操作成功',$rdata);
    }

    /**
     * 直推业绩统计
     * @param JWTAuth $auth
     * @return \Illuminate\Http\JsonResponse
     */
    public function ztYeji(JWTAuth $auth){
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $page_size=Input::get('page_size')??20;
        $page=Input::get('page')??1;

        $tj_type=Input::get('tj_type')??'today';
        //推荐的ids
        $tjdids=[];
        $tjdids=UserModel::where('tuid',$user->id)->pluck('id');

        //团队列表
        $query=UserModel::select('id','username','userphone','realname','nickname','tdeep','created_at','yeji_td','yeji_td_total','yeji_self','yeji_self_total');
        if($tj_type=='today'){
            $query->where('yeji_self','>',0);
        }else{
            $query->where('yeji_self_total','>',0);
        }
        $records=$query->where('tuid',$user->id)->paginate($page_size,$page);
        $records->each(function ($record)use($tj_type){
            if($tj_type=='today'){
                $record->show_yeji=$record->yeji_self;
            }else{
                $record->show_yeji=$record->yeji_self_total;
            }
        });
        $rdata['records']=$records;
        return $this->apiSuccess('操作成功',$rdata);
    }
}
